# -*- coding: utf-8 -*- # 
import wx
#import images
import wx.lib
import wx.lib.stattext
import wx.html
from SortedList import SortedList
from data import get as getAppAttr
import appimage
import ColorPanel
from doubanget import doubanClient

class bookSearchFrame(wx.Dialog):
    def __init__(self,parent = None, id = -1,
                    title = getAppAttr("searchFrameTitle")):
        size = (800,600)
        wx.Dialog.__init__(self,parent,id,title,size=size)
        
        self.initFrame()
        apikey = "23eeeb4347bdd26bfc6b7ee9a3b755dd"
        self.doubanCl = doubanClient(apikey)
        
    def initFrame(self):
        self.searchRltPanel = BookSearchRltPnl(self)
        
        self.btnPanel = wx.Panel(self)
        self.okbtn = okbtn = wx.Button(self.btnPanel,label = getAppAttr("bottonsStr")["00002"])
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(okbtn, 0, wx.EXPAND)
        self.btnPanel.SetSizer(box)
        
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.searchRltPanel, 1, wx.EXPAND)
        box.Add(self.btnPanel, 0, wx.EXPAND)
        self.SetSizer(box)
        
        self.Bind(wx.EVT_BUTTON, self.OnConfirm,self.okbtn)
        self.list = self.searchRltPanel.GetListCtrl()
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelected,self.list)
        self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnDeSelected,self.list)
        self.select = ""
    def OnConfirm(self,evt):
        self.Close()
    def OnSelected(self,evt):
        idx =  evt.GetData()
        self.select = self.searchRltPanel.itemDataMap[idx]
        #print self.select.GetText()
    def OnDeSelected(self,evt):
        self.select = None
        
class BookSearchRltPnl(wx.Panel, SortedList):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        SortedList.__init__(self, parent)
    
        self.load()

    
    def init(self):

        box = wx.BoxSizer(wx.VERTICAL)
    

        self.list = self.createlist( getAppAttr("SearchRltTableTitles")
        )
        box.Add(self.list, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 2)

        self.SetSizer(box)
        self.SetAutoLayout(True)

    def initdata(self):
        datas = {}
        for i in range(5,100):
            datas[i] = ('', i, '书名%02d' % i, '作者%03d' % i,'出版时期%04d' % i)
        return datas
    
    def load(self):
        number = self.loaddata()
        self.SortListItems(col=1)
class TestApp(wx.App):
    def OnInit(self):
        #self.frame = bookSearchFrame(None, -1)
        self.frame = BookSearchDialog(None, -1)
        self.SetTopWindow(self.frame)
        self.frame.Show()
        #result = self.frame.ShowModal()
        return True

class BookSearchDialog(wx.Frame):
    def __init__(self,parent=None,id=-1,title= getAppAttr("searchFrameTitle"),size=(800,600)):
        wx.Frame.__init__(self,parent,id,title=title,size=size)
        self.prepare()
    
    def prepare(self):
        self.panel = wx.Panel(self)
        
        self.pnlSearch = wx.Panel(self.panel)
        
        keywordHigh = 25
        font = wx.Font(10,wx.FONTFAMILY_MODERN,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_BOLD)
        self.pnlSrhKeyword = wx.Panel(self.pnlSearch,size=(-1,keywordHigh))
        self.lblKeyword = wx.lib.stattext.GenStaticText(self.pnlSrhKeyword,-1,
                                        label = u"关键词:",
                                        size=(130,-1),
                                        style = wx.ALIGN_LEFT)
        self.lblKeyword.SetFont(font)
        

        #self.lblKeyword = wx.html.HtmlWindow(self.pnlSrhKeyword,size=(100,20),style = wx.html.HW_SCROLLBAR_NEVER)
        #strKeyword = u"""<td bgcolor="" align=center valign="middle"><b>关键词：</b></td>"""
        #self.lblKeyword.SetPage(strKeyword)
        #self.lblKeyword.SetBackgroundColour("green")
        self.txtKeyword = wx.TextCtrl(parent = self.pnlSrhKeyword,size=(-1,keywordHigh))
        self.btnSearch = wx.Button(parent = self.pnlSrhKeyword,size=(-1,keywordHigh),label = u"搜索")
        
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(self.lblKeyword, 0, wx.ALIGN_CENTER|wx.FIXED_MINSIZE)
        box.Add(self.txtKeyword, 1, wx.EXPAND)
        box.Add(self.btnSearch, 0, wx.EXPAND)
        self.lblKeyword.SetMinSize((130,self.lblKeyword.GetSize()[1]))
        self.pnlSrhKeyword.SetSizer(box)
        
        self.pnlSrhOption = wx.Panel(self.pnlSearch)
        size = (70,30)
        self.chkbOptAll = wx.CheckBox(self.pnlSrhOption,label = u"任意",size = size)
        self.rbOptName = wx.RadioButton(self.pnlSrhOption,-1,label = u"书名",size = size,pos = (100,0),style = wx.RB_GROUP)
        self.rbOptAuthor = wx.RadioButton(self.pnlSrhOption,-1,label = u"作者",size = size)
        self.rbOptTranslator = wx.RadioButton(self.pnlSrhOption,-1,label = u"译者",size = size)
        self.rbOptIsbn = wx.RadioButton(self.pnlSrhOption,-1,label = u"ISBN号",size = size)
        
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(self.chkbOptAll, 0, wx.EXPAND)
        box.Add(self.rbOptName, 0, wx.EXPAND)
        box.Add(self.rbOptAuthor, 0, wx.EXPAND)
        box.Add(self.rbOptTranslator, 0, wx.EXPAND)
        box.Add(self.rbOptIsbn, 0, wx.EXPAND)
        self.pnlSrhOption.SetSizer(box)
        
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.pnlSrhKeyword, 0, wx.EXPAND)
        box.Add(self.pnlSrhOption, 0, wx.EXPAND)
        self.pnlSearch.SetSizer(box)
        
        self.pnlResult = wx.Panel(self.panel)
        
        self.booklistTbl = BookListTbl(self.pnlResult,-1)
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(self.booklistTbl, 1, wx.EXPAND)
        self.pnlResult.SetSizer(box)
        
        self.selectTbl = None
        
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.pnlSearch, 0, wx.EXPAND)
        box.Add(self.pnlResult, 1, wx.EXPAND)
        self.panel.SetSizer(box)
        
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(box)
    def OnSearch(self):
        pass

colourList = [ "Aquamarine", "Black", "Blue", "Blue Violet", "Brown", "Cadet Blue",
               "Coral", "Cornflower Blue", "Cyan", "Dark Grey", "Dark Green",
               "Dark Olive Green",
               ]
class BookListTbl(wx.Listbook):
    def __init__(self, parent, id):
        
        wx.Listbook.__init__(self, parent, id, style=
                            wx.BK_DEFAULT
                            #wx.BK_TOP
                            #wx.BK_BOTTOM
                            #wx.BK_LEFT
                            #wx.BK_RIGHT
                            )
        #self.log = log

        # make an image list using the LBXX images
        il = wx.ImageList(32, 32)
        for x in range(12):
            #obj = getattr(images, 'LB%02d' % (x+1))
            #bmp = obj.GetBitmap()
            bmp = appimage.getNoDataBitmap()
            il.Add(bmp)
        self.AssignImageList(il)

        # Now make a bunch of panels for the list book
        first = True
        imID = 0
        for colour in colourList:
            win = self.makeColorPanel(colour)
            self.AddPage(win, colour, imageId=imID)
            imID += 1
            if imID == il.GetImageCount(): imID = 0
            if first:
                st = wx.StaticText(win.win, -1,
                          "You can put nearly any type of window here,\n"
                          "and the list can be on any side of the Listbook",
                          wx.Point(10, 10))
                first = False

        self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGED, self.OnPageChanged)
        self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGING, self.OnPageChanging)


    def makeColorPanel(self, color):
        p = wx.Panel(self, -1)
        win = ColorPanel.ColoredPanel(p, color)
        p.win = win
        def OnCPSize(evt, win=win):
            win.SetPosition((0,0))
            win.SetSize(evt.GetSize())
        p.Bind(wx.EVT_SIZE, OnCPSize)
        return p


    def OnPageChanged(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.GetSelection()
        #self.log.write('OnPageChanged,  old:%d, new:%d, sel:%d\n' % (old, new, sel))
        print('OnPageChanged,  old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()

    def OnPageChanging(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.GetSelection()
        #self.log.write('OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel))
        print('OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()

if __name__ == "__main__":
    app = TestApp(0)
    app.MainLoop()